<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\user_center\model\user;

use think\Model;

/**
 * 储值 模型
 * @package app\user_center\model
 */
class UcMemberBalance extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_uc_member_balance_journal';
	
	/**
     * 添加订单
     * @param int $uid     用户id
     * @param string $order_code  订单号
     * @param decimal $pay_price 支付金额
     * @author Nanuto 宝石
     * @return array
     */
	public function addData($uid, $order_code, $pay_price, $pay_type = 0, $pay_status = 0){
		$data['uid']                       = $uid;
        $data['order_code']                = $order_code;
        $data['pay_price']                 = $pay_price;
        $data['pay_type']                  = $pay_type;
        $data['pay_status']                = $pay_status;
        $data['pay_expiration_time']       = time()+30*60;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success', $this->id);
        }else{
            return array_out(-1001, 'error');
        }
	}

    /**
     * 添加---dou使用
     * @param $uid
     * @param $order_code
     * @param $pay_order_id
     * @param $pay_price
     * @param $pay_type
     * @param $pay_status
     * @return array
     */
    public function addDataBmr($uid, $order_code,$pay_order_id, $pay_price, $pay_type, $pay_status){
        $data['uid']                       = $uid;
        $data['order_code']                = $order_code;
        $data['pay_order_id']              = $pay_order_id;
        $data['pay_price']                 = $pay_price;
        $data['pay_type']                  = $pay_type;
        $data['pay_status']                = $pay_status;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success', $this->id);
        }else{
            return array_out(-1001, 'error');
        }
    }

	
	/**
     * 用户统计 关联载入（一对一）
     */
    public function getUcMember(){
        return $this -> hasOne('app\user_center\model\user\Member','uid', 'uid');
    }

    /**
     * 获取储值表信息
     */
    public function getInfo($map)
    {
        $info = $this
            -> alias('j')
            -> leftJoin('xm_uc_member u','j.uid = u.uid')
            -> where($map)
            -> field('j.*,u.nick_name')
            -> order('j.create_time desc')
            -> paginate();

        if($info){
            foreach ($info as $key=>$value){
                $info[$key]['log'] = model('user_center/UcMemberBalanceJournalLog', 'model\user')->where('journal_id','=',$value['id'])->order('create_time asc')->column('*');
            }
            $total = [
                'total_price' => 0,
                'total_uid' => 0,
                'total_order' => 0,
                'total_song_price' => 0,
                'total_price_in' => 0,
                'total_price_e' => 0,
            ];
            $item = [];
            foreach ($info as $key => $value) {
                //计算 总价值
                if($value['pay_status'] == 1){
                    $total['total_price'] += $value['pay_price'];
                    if(!isset($item['uid'][$value['uid']])){
                        $item['uid'][$value['uid']] = $value['uid'];
                        $total['total_uid'] += 1;
                    }else{}
                    if(!isset($item['pay_order_id'][$value['pay_order_id']])){
                        $item['pay_order_id'][$value['pay_order_id']] = $value['pay_order_id'];
                        $total['total_order'] += 1;
                    }else{}
                }else{}
            }
            $infos['total'] = $total;
            $infos['info'] = $info;
            return array_out(200, 'success', $infos);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 储值统计-获取全部信息
     * @param $map
     * @return array
     * @throws \think\exception\DbException
     */
    public function getAllInfo($map)
    {
        $where = ' 1=1 ';
        if(!empty($map)){
            foreach ($map as $km=>$vm){
                if($vm[1] == 'eq'){
                    $where .= ' AND '.$vm[0]."=".$vm[2];
                }elseif ($vm[1] == 'between time'){
                    $where .= ' AND ('.$vm[0]."  between '".strtotime($vm[2][0])."' AND '".strtotime($vm[2][1])."')";
                }else{
                    $ty = $vm[1];
                    $where .= ' AND '.$vm[0]." ".$ty." '".$vm[2]."'";
                }

            }
        }
        //交易明细
        $fields = "l.id as lid,l.journal_id,l.uid as luid,l.ine,l.price,l.use_rule,l.log_remark,l.create_time as lcreate_time,u.nick_name";
        $sql = "SELECT ".$fields.",
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.id FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.id FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END id,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.uid FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.uid FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END uid,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.pay_price FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.pay_price FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END pay_price,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.id FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.pay_order_id FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END pay_order_id,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.create_time FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.create_time FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END create_time,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.update_time FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.update_time FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END update_time,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.pay_type FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.pay_type FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END pay_type,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.pay_status FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.pay_status FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END pay_status,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.ordercode FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.order_code FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END order_code,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.pay_expiration_time FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.pay_expiration_time FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END pay_expiration_time,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.update_time FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.payover_time FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END payover_time
                FROM xm_uc_member_balance_journal_log AS l
                LEFT JOIN xm_uc_member as u ON u.uid=l.uid
                WHERE ".$where."
                order by l.create_time desc";
        $info = $this->query($sql);
        foreach ($info as $key_a=>$value_a){
            if($value_a['pay_status'] !== 1){
                unset($info[$key_a]);
            }
        }
        $total = [
            'total_price' => 0,
            'total_uid' => 0,
            'total_order' => 0,
            'total_song_price' => 0,
            'total_price_in' => 0,
            'total_price_e' => 0,
        ];
        if ($info) {
            $item = [];
            foreach ($info as $key => $value) {
                //计算 总价值
                if(!isset($item['uid'][$value['uid']])){
                    $item['uid'][$value['uid']] = $value['uid'];
                    $total['total_uid'] += 1;
                }else{}
                if(!isset($item['pay_order_id'][$value['pay_order_id']])){
                    $item['pay_order_id'][$value['pay_order_id']] = $value['pay_order_id'];
                    $total['total_order'] += 1;
                }else{}
                //收入支出统计
                if($value['ine'] == 3){
                    $total['total_price_e'] += $value['price'];
                }elseif($value['ine'] == 2){
                    $total['total_price_in'] += $value['price'];
                }else{
                    $total['total_price'] += $value['price'];
                }
            }
            $infos['info'] = $info;
        } else {
            $infos['info'] = [];
        }
        $infos['total'] = $total;
        return array_out(200, 'success', $infos);
    }

    /**
     * 储值统计-获取会员全部信息
     */
    public function getAllInfoForPerson($where)
    {
        //交易明细
        $fields = "l.id as lid,l.journal_id,l.uid as luid,l.ine,l.price,l.use_rule,l.log_remark,l.create_time as lcreate_time,u.nick_name";
        if(!empty($where)){
            $sql = "SELECT ".$fields.",
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.id FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.id FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END id,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.uid FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.uid FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END uid,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.pay_price FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.pay_price FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END pay_price,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.id FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.pay_order_id FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END pay_order_id,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.create_time FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.create_time FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END create_time,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.update_time FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.update_time FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END update_time,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.pay_type FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.pay_type FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END pay_type,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.pay_status FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.pay_status FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END pay_status,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.ordercode FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.order_code FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END order_code,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.pay_expiration_time FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.pay_expiration_time FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END pay_expiration_time,
                CASE
                    WHEN l.ine = 3 THEN (SELECT o.update_time FROM xm_erp_goods_order AS o WHERE o.id=l.journal_id limit 1)
                    WHEN l.ine <>3 THEN (SELECT j.payover_time FROM xm_uc_member_balance_journal AS j WHERE j.id=l.journal_id limit 1)
                END payover_time
                FROM xm_uc_member_balance_journal_log AS l
                LEFT JOIN xm_uc_member as u ON u.uid=l.uid
                WHERE ".$where."
                order by l.create_time desc";
            $info = $this->query($sql);
            foreach ($info as $key_a=>$value_a){
                if($value_a['pay_status'] !== 1){
                    unset($info[$key_a]);
                }
            }
            $total = [
                'total_price' => 0,
                'total_uid' => 0,
                'total_order' => 0,
                'total_song_price' => 0,
                'total_price_in' => 0,
                'total_price_e' => 0,
            ];
            if ($info) {
                $item = [];
                foreach ($info as $key => $value) {
                    //计算 总价值
                    if(!isset($item['uid'][$value['uid']])){
                        $item['uid'][$value['uid']] = $value['uid'];
                        $total['total_uid'] += 1;
                    }else{}
                    if(!isset($item['pay_order_id'][$value['pay_order_id']])){
                        $item['pay_order_id'][$value['pay_order_id']] = $value['pay_order_id'];
                        $total['total_order'] += 1;
                    }else{}
                    //收入支出统计
                    if($value['ine'] == 3){
                        $total['total_price_e'] += $value['price'];
                    }elseif($value['ine'] == 2){
                        $total['total_price_in'] += $value['price'];
                    }else{
                        $total['total_price'] += $value['price'];
                    }
                }
                $infos['info'] = $info;
            } else {
                $infos['info'] = [];
            }
            $infos['total'] = $total;
            return array_out(200, 'success', $infos);
        }else{
            return array_out(-1001, 'error');
        }

    }

    

    /**
     * 删除数据
     * @param int $id
     * @author dou<51779987@qq.com>
     * @return bool
     */
    public function deleteData($id){
        $res = self::destroy($id);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
}
